<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 15:22:10
 * @LastEditTime: 2023-09-02 12:21:26
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 兼容老接口数据
 */

namespace App\Http\Controllers\OldController;

use App\Http\Services\Storage\AllocationService;
use App\Http\Services\Storage\ProYarnService;
use App\Http\Services\System\UserService;
use App\Models\BarcodeCount;
use App\Models\User;
use App\Repositories\EmployeeRepository;
use App\Repositories\MpcAllocationDetailRepository;
use App\Repositories\MpcAllocationRepository;
use App\Repositories\ProEmployeeProRepository;
use App\Repositories\ProPlanRepository;
use App\Repositories\ProProcessesWipRepository;
use App\Repositories\ProProDetailRepository;
use Illuminate\Support\Facades\DB;
use Laravel\Lumen\Routing\Controller as BaseController;

/**
 * 老系统兼容接口
 * @property ProYarnService $pro_yarn
 * @property UserService $service
 * @property User $user
 * @author sunkaiyuan
 */
class StorageController extends BaseController
    {

    public function __construct(UserService $service)
        {
        $this->service = $service;
        if (auth()->guard()->guest()) {
            fail('Unauthorized.', 401);
            }
        $this->user = auth()->user();
        }

    public function getBarcode()
        {
        $code           = request("barcode");
        $position       = strripos($code, "b=");
        $position       = $position > 0 ? $position + 2 : 0;
        $code           = substr($code, $position, strlen($code) - $position);
        $code10         = base_convert($code, 36, 10);
        $id             = substr($code10, 0, strlen($code10) - 5);
        $barcode        = BarcodeCount::whereId($id)->first();
        $wip            = ProPlanRepository::whereDyelot($barcode->dyelot)->getOne();
        $wip['barcode'] = $code;
        return $wip;
        }
    public function saveDmBooking()
        {
        $request                = request();
        $data                   = [
            'barcode'     => $request->barcode,
            'material_id' => $request->material_id,
            'color_id'    => $request->color_id,
            'dyelot'      => $request->dyelot,
            'quantity'    => $request->newqtyb,
        ];
        $empoyee_model          = EmployeeRepository::whereId(auth()->user()->employee_id)->first();
        $data['position_id']    = $empoyee_model->position_id; //岗位
        $data['swipor_id']      = $empoyee_model->id; //员工ID
        $data['shift_id']       = $empoyee_model->shift_id; //参数
        $data['swiped_at']      = date("Y-m-d H:i:s");
        $data['process_pro_id'] = (new ProEmployeeProRepository)->add($empoyee_model, $data['quantity'], $data['swiped_at']);
        return ProProDetailRepository::run("autosave", [$data]);
        }
    public function dmBookingList()
        {
        $req = request();
        return ProProDetailRepository::whereSwipor_id(auth()->user()->employee_id)
            ->condition()
            ->forPage($req->pageNum, $req->pageSize)
            ->orderBy("id", "desc")
            ->getList();
        }
    public function deleteDmBooking()
        {

        return ProProDetailRepository::whereId(request("id"))->get()->filter(function ($model)
            {
            if (time() - strtotime($model->swiped_at) > 24 * 3600) {
                fail("超过24小时无法删除！");
                }

            (new ProEmployeeProRepository)->add($model->swipor, 0 - $model->quantity, $model->swiped_at);
            return $model->delete();
            });
        }
    public function dmBookingMonthList()
        {
        return [];
        }

    public function dbList()
        {
        $req = request();
        return MpcAllocationDetailRepository::whereCreator_id(auth()->user()->employee_id)->condition()
            ->forPage($req->pageNum, $req->pageSize)
            ->getList();
        }
    public function dbDelete()
        {
        $id = request("id");
        DB::beginTransaction();
        $details = MpcAllocationDetailRepository::whereId($id)->first();
        AllocationService::noaudit([$details->main_id]);
        AllocationService::delete([$details->main_id]);
        DB::commit();
        }
    }